package com.lagou.sqlsession;

import com.lagou.config.Configuration;
import com.lagou.config.MapperStatement;
import com.lagou.config.MethodType;
import com.lagou.excutor.Executor;
import com.lagou.excutor.SimpleExecutor;

import java.lang.reflect.*;
import java.util.List;

public class DefaultSqlSession implements SqlSession {
    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    public <E> List<E> selectList(String statementId, Object... params) throws Exception {
        Executor simpleExecutor = new SimpleExecutor();
        return simpleExecutor.query(configuration,statementId,params);
    }

    public <T> T selectOne(String statementId, Object... params) throws Exception {
        List<Object> list = selectList(statementId, params);
        if(list != null && list.size() == 1) {
            return (T) list.get(0);
        }else if(list.size() > 1){
            throw new RuntimeException("当前返回结果过多");
        }else {
            return null;
        }
    }

    @Override
    public <T> T getMapper(Class<T> tClass) {
        //利用JDK动态代理生成代理对象
        return (T)Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{tClass}, new InvocationHandler() {
            @Override
            //代理对象执行所有被代理对象的方法都会执行invoke方法
            public Object invoke(Object o, Method method, Object[] args) throws Throwable {
                //包名+类名
                String className = method.getDeclaringClass().getName();
                //方法名
                String methodName = method.getName();
                //mapper.xml中约定namespace为接口的全限定名，接口中的方法名和标签中的id属性一致
                String statementId = className.concat(".").concat(methodName);

                MapperStatement mapperStatement = configuration.getMapperStatementMap().get(statementId);
                MethodType methodType = mapperStatement.getMethodType();
                Object returnVal = null;
                switch (methodType) {
                    case INSERT:
                        returnVal = insert(statementId,args);
                        break;
                    case UPDATE:
                        returnVal = update(statementId,args);
                        break;
                    case DELETE:
                        returnVal = delete(statementId,args);
                        break;
                    case SELECT:
                    {
                        //返回值类型
                        Type genericReturnType = method.getGenericReturnType();
                        //判断返回值类型是参数化类型，例如List<String>
                        if(genericReturnType instanceof ParameterizedType) {
                            returnVal = selectList(statementId, args);
                        }else {
                            returnVal = selectOne(statementId,args);
                        }
                        break;
                    }
                    default:
                        throw new RuntimeException("未知的方法类型");

                }
                return returnVal;

            }
        });
    }

    @Override
    public int insert(String statementId, Object... params) throws Exception {
        return update(statementId,params);
    }

    @Override
    public int update(String statementId, Object... params) throws Exception {
        SimpleExecutor executor = new SimpleExecutor();
        return executor.update(configuration,statementId,params);
    }

    @Override
    public int delete(String statementId, Object... params) throws Exception {
        return update(statementId,params);
    }
}
